package com.kajdo.notes.converter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class NotesCalendar {
	private static String ownerSearch = "Chair:  CN=";
	private static String startDateTimeSearch = "STARTDATETIME:  ";
	private static String endDateTimeSearch = "EndDateTime:  ";
	private static String subjectSearch = "Subject:  ";
	private static String locationSearch = "Location:  ";
	private static String descSearch = "__________________";

	private ArrayList<CalendarEntry> notesEntries = new ArrayList<CalendarEntry>();

	public NotesCalendar(String inputFileName) {
		String line;
		File inputFile;
		FileReader fr;
		BufferedReader br;

		try {

			CalendarEntry entry = new CalendarEntry();

			inputFile = new File(inputFileName);
			fr = new FileReader(inputFile);
			br = new BufferedReader(fr);

			line = br.readLine();

			while (line != null) {

				if (line.startsWith(ownerSearch))
					entry.setOwner(line.substring(ownerSearch.length()));
				if (line.startsWith(startDateTimeSearch))
					entry.setStartDateTime(line.substring(startDateTimeSearch
							.length()));
				if (line.startsWith(endDateTimeSearch))
					entry.setEndDateTime(line.substring(endDateTimeSearch
							.length()));
				if (line.startsWith(subjectSearch))
					entry.setSubject(line.substring(subjectSearch.length()));
				if (line.startsWith(locationSearch))
					entry.setLocation(line.substring(locationSearch.length()));
				if (line.startsWith(descSearch)) {
					//2 zeilen raus
					line = br.readLine();
					line = br.readLine();
					
					while (!(line.equals("\f"))) {
						if (entry.getDesc() != null)
							entry.setDesc(entry.getDesc() + "\n" + line);
						else
							entry.setDesc(line);
						line = br.readLine();
					}
				}
				if (line.equals("\f")) {
					notesEntries.add(entry);
					entry = new CalendarEntry();
				}

				line = br.readLine();
			}
			br.close();
		} catch (FileNotFoundException fnfe) {
			System.out.println("FileNotFound: " + fnfe);
		} catch (IOException ioe) {
			System.out.println("IOException: " + ioe);
		}
	}

	public ArrayList<CalendarEntry> getNotesEntries() {
		return notesEntries;
	}
}
